﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetNuke.Services.Localization;
namespace iFinity.DNN.Modules.CacheMaster
{
    internal class UIController
    {
        internal static string GetResourceString(string localResourceFile, string key, string defaultValue)
        {
            string result = Localization.GetString(localResourceFile, key);
            if (result == null)
                result = defaultValue;
            return result;
        }
        internal static string GetCacheItemUrl(int tabId, int moduleId, string cacheKey)
        {
            string url = DotNetNuke.Common.Globals.NavigateURL(tabId, "Detail", "mid", moduleId.ToString());
                //, "cacheKey", System.Web.HttpUtility.UrlEncode(cacheKey));
            //add on as querystring to prevent Url errors
            if (url.Contains("?"))
                url += "&";
            else

                url += "?";
            url += "cacheKey=" + System.Web.HttpUtility.UrlEncode(cacheKey);
            return url;
        }
        internal static string FormatUTCDate(int timezoneOffset, DateTime utcDate)
        {
            return FormatUTCDate(timezoneOffset, utcDate, DateTime.MinValue);
        }

        internal static string FormatUTCDate(int timezoneOffset, DateTime utcDate, DateTime fromDate)
        {
            string value = "";
            if (utcDate > DateTime.MinValue)
            {
                if (utcDate < DateTime.MaxValue)
                {
                    DateTime localTime = utcDate.AddMinutes((double)timezoneOffset);
                    value = localTime.ToShortTimeString();
                    if (fromDate > DateTime.MinValue)
                    {
                        //add timespan to end
                        TimeSpan diff = utcDate - fromDate;
                        value += " ( +" + FormatTimespan(diff) + ")";
                    }
                }
                else
                    value = "Unlimited";
            }
            else
                value = "N/A";

            return value;
        }
        internal static string FormatTimespan(TimeSpan ts)
        {
            string output = "";
            if (ts.TotalDays > 0 && ts.Days > 0)
            {
                if (ts.Days == 1)
                    output += ts.Days.ToString() + " Day ";
                else
                    output += ts.Days.ToString() + " Days ";
            }
            if (ts.TotalHours > 0 && ts.Hours > 0)
            {
                if (ts.Hours == 1)
                    output += ts.Hours.ToString() + " hr ";
                else
                    output += ts.Hours.ToString() + " hrs ";
            }
            if (ts.TotalMinutes > 0 && ts.Minutes > 0)
            {
                if (ts.Minutes ==1)
                    output += ts.Minutes.ToString() + " min ";
                else
                    output += ts.Minutes.ToString() + " mins ";
            }
            if (ts.TotalSeconds > 0 && ts.Seconds > 0)
            {
                if (ts.TotalSeconds == 1)
                    output += ts.Seconds.ToString() + " sec ";
                else
                    output += ts.Seconds.ToString() + " secs ";
            }
            if (ts.TotalMilliseconds > 0 && ts.Milliseconds > 0)
            {
                output += ts.Milliseconds.ToString() + " ms";
            }
            return output;

            
        }
    }
}
